package tbb

import (
	"fmt"
	"github.com/spf13/cobra"
	"go-blockchain-from-scratch/constants"
	"go-blockchain-from-scratch/database"
	"go-blockchain-from-scratch/mapper"
	"go.mongodb.org/mongo-driver/bson"
)

func incorrectUsageErr() error {
	return fmt.Errorf("incorrect usage")
}

var balancesListCmd = &cobra.Command{
	Use:   "list",
	Short: "Lists all balances.",
	Run: func(cmd *cobra.Command, args []string) {
		states := make([]database.State, 0)
		mapper.Select(constants.DataBase, constants.StateCollection, bson.M{}, &states)
		fmt.Println("Accounts balances:")
		fmt.Println("__________________")
		fmt.Println("")
		for _, state := range states {
			fmt.Println(fmt.Sprintf("%s: %d", state.Name, state.Balances))
		}
	},
}

func BalancesCmd() *cobra.Command {
	balancesCmd := &cobra.Command{
		Use:   "balances",
		Short: "Interact with balances (list...).",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			return incorrectUsageErr()
		},
		Run: func(cmd *cobra.Command, args []string) {
		},
	}

	balancesCmd.AddCommand(balancesListCmd)

	return balancesCmd
}
